package com.bw.controller.orders;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bw.entity.customer.PiCustomer;
import com.bw.entity.material.PiMaterial;
import com.bw.entity.orders.PiOrder;
import com.bw.entity.orders.PiOrderItem;
import com.bw.entity.user.TbUser;
import com.bw.req.OrderItemCom;
import com.bw.resp.ResponseResult;
import com.bw.service.customer.IPiCustomerService;
import com.bw.service.linkman.ILinkmanService;
import com.bw.service.orders.IPiOrderItemService;
import com.bw.service.orders.IPiOrderService;
import com.bw.service.user.ITbUserService;
import com.bw.utils.excel.ExcelUtil;
import net.sf.excelutils.ExcelUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.core.io.ClassPathResource;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  上传BOM前端控制器
 * </p>
 *
 * @author lizhengqian
 * @since 2020-06-19
 */
@RestController
@RequestMapping("orderItem")
public class PiOrderItemController {

    @Autowired
    private IPiOrderService orderService;

    @Autowired
    private IPiOrderItemService orderItemService;

    @Autowired
    private IPiCustomerService customerService;

    @Autowired
    private ITbUserService userService;

    private final Logger logger = LoggerFactory.getLogger(PiOrderItemController.class);

    /**
     * @Description:上传BOM
     * @Author: 周明丽
     * @Date: 2020/6/20 9:39
     * @param file:
     * @return: com.bw.resp.ResponseResult
     **/
    @PostMapping("importBom")
    public ResponseResult importBom(MultipartFile file) {
        logger.info("上传BOM开始");
        ResponseResult result = ResponseResult.SUCCESS();

        try {
            //获取后缀
            String ext=file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."),file.getOriginalFilename().length());
            List<Map> orderItemList = ExcelUtil.readExcel(ext,file.getInputStream());
            //将集合转成字符串
            String jsonString= JSON.toJSONString(orderItemList);
            //将字符串转成对象
            List<PiOrderItem> piOrderItems = JSON.parseArray(jsonString,PiOrderItem.class);
            //进行添加
            result.setResult(piOrderItems);

        }catch (Exception e){
            logger.error("导入BOM失败:" + e.toString());
            result.setSuccess(false);
            result.setMessage("导入BOM失败:" + e.toString());
        }
        logger.info("上传BOM结束");
        return result;
    }


    /**
     * @Description:查询
     * @Author: 周明丽
     * @Date: 2020/7/1 19:27
     * @param id:
     * @return: com.bw.resp.ResponseResult
     **/
    @GetMapping("/getOrderItemList/{id}")
    public ResponseResult getOrderItemList(@PathVariable("id") long id) {
        logger.info("开始根据id查找到零件");
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            PiOrderItem orderItem = orderItemService.getById(id);
            result.setResult(orderItem);
        } catch (Exception e) {
            result.setMessage("查询失败" + e.toString());
            result.setSuccess(false);
            logger.error("查询错误" + e.toString());
        }
        logger.info("开始根据id查找到零件结束");
        return result;

    }
    /**
     * @Description:根据图号进行回显订单零件信息
     * @Author: 周明丽
     * @Date: 2020/6/23 10:41
     * @param id:
     * @return: com.bw.resp.ResponseResult
     **/
    @GetMapping("getById/{id}")
    public ResponseResult getById(@PathVariable("id") Long id) {
        ResponseResult result = ResponseResult.SUCCESS();
        logger.info("原材料回显开始");
        try {
            QueryWrapper queryWrapper=new QueryWrapper();
            queryWrapper.eq("code",id);
            PiOrderItem piOrderItem = orderItemService.getOne(queryWrapper);
            result.setResult(piOrderItem);
            result.setMessage("查询原材料成功");
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("查询原材料失败:" + e.toString());
        }
        logger.info("原材料回显结束");
        return result;
    }





}
